<?php
namespace app\index\model;
use think\Model;
use think\Db;
class IndexModel extends Model{
	function getGoods($order_id){
		$this->setSellStatus($order_id);
					$n2 = array(
					    'order_id' => $order_id,
					    'status'=>1
					);
					$menu=array();
					$menu = Db::name('waimai_sell_log')->field('goods_id,name,price,guige,num,gkid,gwid,status')->where($n2)->select();
					
					    if (!empty($menu['0'])) {
					       
					        foreach ($menu as $key => $vo) {
					        	
								$menu[$key]['allprice'] = ($menu[$key]['price'])*($menu[$key]['num']);
					            
					           if(empty($vo['name'])){
					
					                $goods = Db::name('waimai_goods')->field('goods_id,name,price,unit,gjson,gweight')->where('goods_id', $vo['goods_id'])->find();
					                if (!empty($goods)) {
					                 
					                            $menu[$key]['name'] = $goods['name'];
					                            $menu[$key]['price'] = $goods['price'];
					                             
					                            //获取一元购的菜品规格一
					                            $gjson = json_decode($goods['gjson'], true);
					
					                            if (!empty($gjson[$vo['gkid'] - 1])) {
					                                $menu[$key]['name'] = $gjson[$vo['gkid'] - 1]['0'];
					                                $menu[$key]['price'] = $gjson[$vo['gkid'] - 1]['1'];
					                            } else if (!empty($gjson['0'])) {
					                                $menu[$key]['name'] = $gjson['0']['0'];
					                                $menu[$key]['price'] = $gjson['0']['1'];
					                            }
					
					                            //获取一元购的菜品规格二
					                            $gweight = json_decode($goods['gweight'], true);
					               // dump($gweight);
					                            if (!empty($gweight[$vo['gwid'] - 1])) {
					                               $cout=$gweight[$vo['gwid'] - 1]['0'];
					                               $ke=$gweight[$vo['gwid'] - 1]['1'];
					                                $menu[$key]['guige'] = $cout;
					                                if($gweight[$vo['gwid'] - 1]['1']=="两"){
					                                	$menu[$key]['allprice']=(($menu[$key]['price'])/10)*(double)$cout;
					                                }else{
					                                	$menu[$key]['allprice']=($menu[$key]['price'])*(double)$cout*($menu[$key]['num']);
					                                }
					                                if(!empty($ke)){
					                                	$menu[$key]['allprice']=($menu[$key]['price']/500)*$ke*$menu[$key]['num'];
					                                }else{
					                               	 $menu[$key]['allprice']=($menu[$key]['price'])*(double)($gweight[$vo['gwid'] - 1]['0'])*($menu[$key]['num']);	
					                                }
					                                
					                            	
					                            	
					                            } else {
					                              //  echo "not null 3:". $vo['gwid'];
					                                $menu[$key]['guige'] = $goods['unit'];
					                                $menu[$key]['allprice']=($menu[$key]['price'])*($menu[$key]['num']);
					                            }
					                    
					                }
					            }
					        }
					    }
	
      				return $menu;
	}
	function setSellStatus($order_id){
		$n = array(
					    'order_id' => $order_id,
					);
					$menu1=array();
					$menu1 = Db::name('waimai_sell_log')->field('goods_id,status')->where($n)->select();
				if(!empty($menu1['0'])){
					
					foreach ($menu1 as $key => $vo) {
					        		$whstatus=array(
					        			'order_id'=>$order_id,
					        			'goods_id'=>$vo['goods_id']
					        		);
					        		$data_status=[
					        			'status'=>1
					        		];
					        		Db::name('waimai_sell_log')->where($whstatus)->update($data_status); 
					}
				}
	}
function cgdelete($order_id){	
	$order=Db::name('waimai_order')->where('order_id',$order_id)->find();
		$menu=$this->getGoods($order_id);
		$flag=0;
	foreach($menu as $m){	
					$whcg=array(
						'goods_name'=>$m['name'],
						'ps_date'=>$order['ps_date'],
						'ps_time'=>$order['ps_time'],
					);
					$cggoods=Db::name('waimai_caigou_list')->where($whcg)->find();
		if(!empty($cggoods)){
				$cgguige=$cggoods['guige'];
				$cgdesc=$cggoods['desc'];
				$cgnum=$cggoods['num'];
			if($m['num']==$cgnum){
					Db::name('waimai_caigou_list')->where($whcg)->delete();
					
					$flag=1;
			}else{
				$num=$m['num']."*";
				$num1=$cgnum-$m['num'];
				$gg=$num.$m['guige'];
				$c=strrchr($cgguige,$gg);
				if(strpos($c,"|")>0){
					$guige1=str_replace($gg."|",'',$cgguige);
				}else{
					$guige1=str_replace($gg,'',$cgguige);
				}
				$nof=strpos($cgdesc,$num);//数字第一次出现位置
				$chr=strrchr($cgdesc,$num);//截取数字后面的所有字符串
				$dof=strpos($chr,",");
				$ddesc=substr($cgdesc,$nof,$dof+1);
				$desc1=str_replace($ddesc,'',$cgdesc);
						echo $nof."dof:".$dof."<br/>";
						echo "chr:".$chr."<br/>";
						echo $ddesc;
				
				
				$data=[
				'num'=>$num1,
				'guige'=>$guige1,
				'desc'=>$desc1
				];
				Db::name('waimai_caigou_list')->where($whcg)->update($data);
				$flag=1;
			
			}
		}
	}
			return $flag;
}
function cgupdate($order_id,$goods_id){
	$order=Db::name('waimai_order')->where('order_id',$order_id)->find();
		$menu=$this->getGoods($order_id);
		$flag=0;
		foreach($menu as $m){
				if($m['goods_id']==$goods_id){
				$whcg=array(
					'goods_name'=>$m['name'],
					'ps_date'=>$order['ps_date'],
					'ps_time'=>$order['ps_time'],
					'cg_status'=>3
				);
				$cggoods=Db::name('waimai_caigou_list')->where($whcg)->find();
				$cgguige=$cggoods['guige'];
				$cgdesc=$cggoods['desc'];
				$cgnum=$cggoods['num'];
				if($m['num']==$cgnum){
				Db::name('waimai_caigou_list')->where($whcg)->delete();
				$flag=1;
				}else{
				$num=$m['num']."*";
				$num1=$cgnum-$m['num'];
				$gg=$num.$m['guige'];
				$c=strrchr($cgguige,$gg);
				if(strpos($c,"|")>0){
					$guige1=str_replace($gg."|",'',$cgguige);
				}else{
					$guige1=str_replace($gg,'',$cgguige);
				}
				$nof=strpos($cgdesc,$num);//数字第一次出现位置
				$chr=strrchr($cgdesc,$num);//截取数字后面的所有字符串
				$dof=strpos($chr,",");
				$ddesc=substr($cgdesc,$nof,$dof+1);
				$desc1=str_replace($ddesc,'',$cgdesc);
//						echo $nof."dof:".$dof."<br/>";
//						echo "chr:".$chr."<br/>";
//						echo $ddesc;
				
				
				$data=[
				'num'=>$num1,
				'guige'=>$guige1,
				'desc'=>$desc1
				];
				Db::name('waimai_caigou_list')->where($whcg)->update($data);
				$flag=1;
				}
				}
		}
	return $flag;	
}
	
	
	function getDeliver($order_id){
		$deliver=array();
		$oid = array(
    					'order_id'=>$order_id
    				);
      	$deliver_uid = Db::name('deliver_supply')->field('uid')->where($oid)->find(); 
      							
      	if(!empty($deliver_uid)){
      			$deliver=Db::name('deliver_user')->field('uid,name')->where('uid',$deliver_uid['uid'])->find();
      	}
      	return $deliver;
	}
	function getSellLog($order_id){
		$n = array(
					    'order_id' => $order_id,
					    'status'=>1
					);
					$sell=array();
					$sell = Db::name('waimai_sell_log')->field('goods_id,name,price,guige,num,gkid,gwid,status')->where($n)->select();
					
      	return $sell;
	}
	function getAddress($address){
		$addr=array();
		//反序列化地址
			if(strlen($address)>8){
				$addr = unserialize($address);
				if(empty($addr['detail'])){
					$addr['detail']="";
				}
				$addr_zhen=explode("镇",$addr['address']);
				if(count($addr_zhen)>1){
					$addr['address']=$addr_zhen['1'];
				}else{
					$addr_shi=explode("市",$addr['address']);
					if(count($addr_shi)>1){
						$addr['address']=$addr_shi['1'];
					}
				}
			}else{
					$addr['detail']="";
					$addr['address']="";
			}
		return $addr;	
	}
	function cgcount(){
		
	     $cgcount = 0;
	     $time1=strtotime(date("Y-m-d"))-21600;//昨天下午6点后
	    $where=array(
	        'cg_status'=>3,
	        'ps_date'=>['>=',$time1]
	    );
	    $cg = Db::name('waimai_caigou_list')->field('cg_number')->where($where)->group('cg_number')->select();
	    if(!empty($cg)){
	        $cgcount = count($cg);
	    }
		return $cgcount;
	}
	function pscount(){
		//获取状态为3的待配送列表
    			$psdate=strtotime(date("Y-m-d"))-21600;//减去6小时
    			$psd_start=strtotime(date("Y-m-d"));
    			$where3 = array(
    				'order_status'=>3,
    				'ps_date'=>$psd_start 
    			);
    			$list = Db::name('waimai_order')->where($where3)->order('ps_time')->select();
    			$ps_list=array();
    			foreach($list as $k=>$s){
    				$ps_list[$s['order_id']]=$s;
    			}
    			//按下单日期取出
    			$where3 = array(
    				'order_status'=>3,
    				'create_time'=>['>=',$psdate]
    			);
    			$clist = Db::name('waimai_order')->where($where3)->order('ps_time')->select();
    			foreach($clist as $k=>$s){
    				if(empty($ps_list[$s['order_id']])){
    					$ps_list[$s['order_id']]=$s;
    				}
    				
    			}
	    		$cout=count($ps_list);
	    return $cout;
	}
	function getPowersByUid($uid){
		$whadmin=array(
						'uid'=>$uid
					);
		$power=Db::name('jtsc_admin')->where($whadmin)->find();
		$powers_list=array();
		if(!empty($power)){
			$power=$power['power'];	
			$pids=explode(",",$power);
			foreach($pids as $key=>$p){
				if($p==1){
					$where=array(
					'parent_id'=>1
					);
					$pid_name=Db::name("jtsc_power")->where($where)->select();
					foreach($pid_name as $name){
						$powers_list[$name['en_name']]=$name['en_name'];
					}
					$powers_list['super']='super';
				}else{
					$where=array(
					'pid'=>$p
					);
				}
					$pid_name=Db::name("jtsc_power")->where($where)->find();
					$powers_list[$pid_name['en_name']]=$pid_name['en_name'];
					
			}				
		}
		
		return $powers_list;
	}
	
	function getAllStore($mer_id){
		$where=array(
		'mer_id'=>$mer_id
		);
		$store_list=Db::name('merchant_store')->where($where)->select();
		return $store_list;
	}
	
}
?>